<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>3 数据处理 | 编程语言笔记</title>
  <meta name="description" content="3 数据处理 | 编程语言笔记" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="3 数据处理 | 编程语言笔记" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="3 数据处理 | 编程语言笔记" />
  
  
  

<meta name="author" content="云腾足下" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="Julia.html"/>
<link rel="next" href="统计.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  jax: ["input/TeX","output/SVG"],
  extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
  TeX: {
    extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
  }
});
</script>
<script type="text/javascript"
   src="../../../MathJax/MathJax.js">
</script>


<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>序</a></li>
<li class="chapter" data-level="1" data-path="Maple.html"><a href="Maple.html"><i class="fa fa-check"></i><b>1</b> Maple</a><ul>
<li class="chapter" data-level="1.1" data-path="Maple.html"><a href="Maple.html#一句话tips"><i class="fa fa-check"></i><b>1.1</b> 一句话Tips</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="Julia.html"><a href="Julia.html"><i class="fa fa-check"></i><b>2</b> Julia</a></li>
<li class="chapter" data-level="3" data-path="DataProcess.html"><a href="DataProcess.html"><i class="fa fa-check"></i><b>3</b> 数据处理</a><ul>
<li class="chapter" data-level="3.1" data-path="DataProcess.html"><a href="DataProcess.html#一句话tips-1"><i class="fa fa-check"></i><b>3.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="3.2" data-path="DataProcess.html"><a href="DataProcess.html#rjsdmx包下载世界各大数据库数据"><i class="fa fa-check"></i><b>3.2</b> <code>RJSDMX</code>包下载世界各大数据库数据</a></li>
<li class="chapter" data-level="3.3" data-path="DataProcess.html"><a href="DataProcess.html#前向后向线性和样条插值"><i class="fa fa-check"></i><b>3.3</b> 前向、后向、线性和样条插值</a><ul>
<li class="chapter" data-level="3.3.1" data-path="DataProcess.html"><a href="DataProcess.html#signal包"><i class="fa fa-check"></i><b>3.3.1</b> <code>signal</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="统计.html"><a href="统计.html"><i class="fa fa-check"></i><b>4</b> 统计</a><ul>
<li class="chapter" data-level="4.1" data-path="统计.html"><a href="统计.html#一句话tips-2"><i class="fa fa-check"></i><b>4.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="4.2" data-path="统计.html"><a href="统计.html#mcmc算法"><i class="fa fa-check"></i><b>4.2</b> MCMC算法</a><ul>
<li class="chapter" data-level="4.2.1" data-path="统计.html"><a href="统计.html#吉布斯抽样原理"><i class="fa fa-check"></i><b>4.2.1</b> 吉布斯抽样原理</a></li>
<li class="chapter" data-level="4.2.2" data-path="统计.html"><a href="统计.html#一些共轭先验分布的结论"><i class="fa fa-check"></i><b>4.2.2</b> 一些共轭先验分布的结论</a></li>
<li class="chapter" data-level="4.2.3" data-path="统计.html"><a href="统计.html#一个吉布斯抽样的典型案例"><i class="fa fa-check"></i><b>4.2.3</b> 一个吉布斯抽样的典型案例</a></li>
<li class="chapter" data-level="4.2.4" data-path="统计.html"><a href="统计.html#metropolis-和-m-h算法"><i class="fa fa-check"></i><b>4.2.4</b> Metropolis 和 M-H算法</a></li>
<li class="chapter" data-level="4.2.5" data-path="统计.html"><a href="统计.html#一些带贝叶斯估计的r包使用报告"><i class="fa fa-check"></i><b>4.2.5</b> 一些带贝叶斯估计的R包使用报告</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">编程语言笔记</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="DataProcess" class="section level1">
<h1><span class="header-section-number">3</span> 数据处理</h1>
<div id="一句话tips-1" class="section level2">
<h2><span class="header-section-number">3.1</span> 一句话Tips</h2>
<ul>
<li>因子操作</li>
</ul>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="DataProcess.html#cb6-1"></a><span class="co"># 使用字符串有两个缺陷：第一，不在因子水平范围内的不会转化成NA</span></span>
<span id="cb6-2"><a href="DataProcess.html#cb6-2"></a><span class="co"># 第二，仅按字母排序。</span></span>
<span id="cb6-3"><a href="DataProcess.html#cb6-3"></a><span class="co"># 因此，通过设定因子水平，可以解决上述两个问题。注意水平和字符串是一样的，</span></span>
<span id="cb6-4"><a href="DataProcess.html#cb6-4"></a><span class="co"># 只是相当于设定了范围和排序。</span></span>
<span id="cb6-5"><a href="DataProcess.html#cb6-5"></a>    <span class="kw">factor</span>(<span class="kw">c</span>(<span class="st">&#39;Dec&#39;</span>,<span class="st">&#39;Apr&#39;</span>,<span class="st">&#39;Jam&#39;</span>,<span class="st">&#39;Mar&#39;</span>), <span class="dt">levels =</span> (<span class="st">&#39;Jan&#39;</span>,<span class="st">&#39;Feb&#39;</span>,<span class="st">&#39;Mar&#39;</span>,<span class="st">&#39;Apr&#39;</span>,<span class="st">&#39;May&#39;</span>))</span>
<span id="cb6-6"><a href="DataProcess.html#cb6-6"></a><span class="co"># 因子重编码, 把1改成unmarried等</span></span>
<span id="cb6-7"><a href="DataProcess.html#cb6-7"></a>farcats<span class="op">::</span><span class="kw">fct_recode</span>(rawdata<span class="op">$</span>marrige,</span>
<span id="cb6-8"><a href="DataProcess.html#cb6-8"></a>           <span class="st">&#39;unmarried&#39;</span>=<span class="st">&#39;1&#39;</span>,<span class="st">&#39;married&#39;</span>=<span class="st">&#39;2&#39;</span>,<span class="st">&#39;cohabitation&#39;</span>=<span class="st">&#39;3&#39;</span>,<span class="st">&#39;divore&#39;</span>=<span class="st">&#39;4&#39;</span>,<span class="st">&#39;wid&#39;</span>=<span class="st">&#39;5&#39;</span>)</span></code></pre></div>
<ul>
<li><code>dbplyr</code>可以连接到几乎任何数据库。</li>
<li><code>wbstats</code>下载世界银行数据，很牛逼。Stata里面的<code>wbopendata</code>包更牛逼。</li>
<li><code>stationaRy</code>:一个从NOAA上下载气象数据，如气温，风向等的包。该包就三个函数，一个用来得到站点id，一个用这个id下载数据，还有一个是如果你想得到其他额外的气象数据时可能有用。</li>
<li><strong>当你发现你用<code>save</code>命令保存一个数据长达数分钟时</strong>，建议你迅速调用<code>qs</code>包，可能一分钟不到就帮你快速读入和保存了。但这个包一次只能保存一个变量。</li>
<li><code>tor</code>: 提供允许用户同时导入多个文件的功能.</li>
<li>读入excel中的sheet名：<code>openxlsx::getSheetNames(file)</code></li>
<li><code>XLConect</code>处理excel最强大的包。但需要JRE（java run enviornment）。</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="DataProcess.html#cb7-1"></a><span class="co"># 可以不改变原有数据，然后把一个数据框精准地写入某个地方</span></span>
<span id="cb7-2"><a href="DataProcess.html#cb7-2"></a><span class="kw">writeWorksheetToFile</span>(<span class="st">&quot;XLConnectExample2.xlsx&quot;</span>, <span class="dt">data =</span> ChickWeight,</span>
<span id="cb7-3"><a href="DataProcess.html#cb7-3"></a> <span class="dt">sheet =</span> <span class="st">&quot;chickSheet&quot;</span>, <span class="dt">startRow =</span> <span class="dv">3</span>, <span class="dt">startCol =</span> <span class="dv">4</span>,<span class="dt">header =</span> <span class="ot">FALSE</span>, <span class="dt">clearSheets =</span> <span class="ot">FALSE</span>)</span></code></pre></div>
<ul>
<li><p>使用<code>as.Date</code>来生成日期，必须带有年月日三个要素，使用<code>format</code>来输出日期格式，此时可以只输出年和月。如<code>as.Date('2010/05/01') %&gt;% format(.,format = '%Y%m')</code></p></li>
<li><p><code>seq.Date()</code>生成日期序列，包括日、星期、月、年。</p></li>
<li><p><code>readstata13</code>包可以读入更高版本的stata数据格式。</p></li>
<li><p><code>zoo::rollapply(x, 30, mean)</code>就是30天的移动平均求值。</p></li>
<li><p><code>select</code>是一个很牛逼的函数</p></li>
</ul>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="DataProcess.html#cb8-1"></a><span class="kw">select</span>(regdata,id, year) <span class="co"># 选择regdata数据框的id和year两列</span></span>
<span id="cb8-2"><a href="DataProcess.html#cb8-2"></a><span class="kw">select</span>(regdata,<span class="kw">starts_with</span>(<span class="st">&#39;abc&#39;</span>)) <span class="co"># 匹配以&#39;abc&#39;开头的列</span></span>
<span id="cb8-3"><a href="DataProcess.html#cb8-3"></a><span class="kw">select</span>(regdata,<span class="kw">ends_with</span>(<span class="st">&#39;abc&#39;</span>)) <span class="co"># 匹配以&#39;abc&#39;结尾的列</span></span>
<span id="cb8-4"><a href="DataProcess.html#cb8-4"></a><span class="kw">select</span>(regdata,<span class="kw">contains</span>(<span class="st">&#39;abc&#39;</span>)) <span class="co"># 匹配包含&#39;abc&#39;的列</span></span>
<span id="cb8-5"><a href="DataProcess.html#cb8-5"></a><span class="kw">select</span>(regdata,<span class="kw">matches</span>(<span class="st">&#39;abc&#39;</span>)) <span class="co"># 正则表达匹配</span></span>
<span id="cb8-6"><a href="DataProcess.html#cb8-6"></a><span class="kw">select</span>(regdata,<span class="kw">num_range</span>(<span class="st">&#39;x&#39;</span>,<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>)) <span class="co"># 匹配x1, x2,x3的列</span></span></code></pre></div>
<ul>
<li>R语言给数组各维数命名</li>
</ul>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="DataProcess.html#cb9-1"></a><span class="co"># Create two vectors of different lengths.</span></span>
<span id="cb9-2"><a href="DataProcess.html#cb9-2"></a>vector1 &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="dv">5</span>,<span class="dv">9</span>,<span class="dv">3</span>)</span>
<span id="cb9-3"><a href="DataProcess.html#cb9-3"></a>vector2 &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="dv">10</span>,<span class="dv">11</span>,<span class="dv">12</span>,<span class="dv">13</span>,<span class="dv">14</span>,<span class="dv">15</span>)</span>
<span id="cb9-4"><a href="DataProcess.html#cb9-4"></a>column.names &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;COL1&quot;</span>,<span class="st">&quot;COL2&quot;</span>,<span class="st">&quot;COL3&quot;</span>)</span>
<span id="cb9-5"><a href="DataProcess.html#cb9-5"></a>row.names &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;ROW1&quot;</span>,<span class="st">&quot;ROW2&quot;</span>,<span class="st">&quot;ROW3&quot;</span>)</span>
<span id="cb9-6"><a href="DataProcess.html#cb9-6"></a>matrix.names &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;Matrix1&quot;</span>,<span class="st">&quot;Matrix2&quot;</span>)</span>
<span id="cb9-7"><a href="DataProcess.html#cb9-7"></a></span>
<span id="cb9-8"><a href="DataProcess.html#cb9-8"></a><span class="co"># Take these vectors as input to the array.</span></span>
<span id="cb9-9"><a href="DataProcess.html#cb9-9"></a>result &lt;-<span class="st"> </span><span class="kw">array</span>(<span class="kw">c</span>(vector1,vector2),<span class="dt">dim =</span> <span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">3</span>,<span class="dv">2</span>),<span class="dt">dimnames =</span> <span class="kw">list</span>(row.names,column.names,</span>
<span id="cb9-10"><a href="DataProcess.html#cb9-10"></a>                                                                  matrix.names))</span>
<span id="cb9-11"><a href="DataProcess.html#cb9-11"></a><span class="kw">print</span>(result)</span></code></pre></div>
<ul>
<li><code>pdftools</code>包的函数可以读PDF文件：</li>
</ul>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="DataProcess.html#cb10-1"></a><span class="kw">pdf_info</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span>
<span id="cb10-2"><a href="DataProcess.html#cb10-2"></a></span>
<span id="cb10-3"><a href="DataProcess.html#cb10-3"></a><span class="kw">pdf_text</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span>
<span id="cb10-4"><a href="DataProcess.html#cb10-4"></a></span>
<span id="cb10-5"><a href="DataProcess.html#cb10-5"></a><span class="kw">pdf_data</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span>
<span id="cb10-6"><a href="DataProcess.html#cb10-6"></a></span>
<span id="cb10-7"><a href="DataProcess.html#cb10-7"></a><span class="kw">pdf_fonts</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span>
<span id="cb10-8"><a href="DataProcess.html#cb10-8"></a></span>
<span id="cb10-9"><a href="DataProcess.html#cb10-9"></a><span class="kw">pdf_attachments</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span>
<span id="cb10-10"><a href="DataProcess.html#cb10-10"></a></span>
<span id="cb10-11"><a href="DataProcess.html#cb10-11"></a><span class="kw">pdf_toc</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span>
<span id="cb10-12"><a href="DataProcess.html#cb10-12"></a></span>
<span id="cb10-13"><a href="DataProcess.html#cb10-13"></a><span class="kw">pdf_pagesize</span>(pdf, <span class="dt">opw =</span> <span class="st">&quot;&quot;</span>, <span class="dt">upw =</span> <span class="st">&quot;&quot;</span>)</span></code></pre></div>
<p>同时，利用<code>qpdf</code>包的<code>pdf_subset,pdf_combine,pdf_split</code>可以提取PDF的部分内容，合并PDF文件，把每一页分成一个PDF文件。</p>
</div>
<div id="rjsdmx包下载世界各大数据库数据" class="section level2">
<h2><span class="header-section-number">3.2</span> <code>RJSDMX</code>包下载世界各大数据库数据</h2>
<p>一般工作流：</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="DataProcess.html#cb11-1"></a><span class="kw">library</span>(RJSDMX)</span>
<span id="cb11-2"><a href="DataProcess.html#cb11-2"></a><span class="co"># 查看有哪些库可以用</span></span>
<span id="cb11-3"><a href="DataProcess.html#cb11-3"></a><span class="kw">getProviders</span>()</span>
<span id="cb11-4"><a href="DataProcess.html#cb11-4"></a><span class="co"># 库中有哪些子库可以用</span></span>
<span id="cb11-5"><a href="DataProcess.html#cb11-5"></a><span class="kw">getFlows</span>(<span class="st">&#39;WITS&#39;</span>)</span>
<span id="cb11-6"><a href="DataProcess.html#cb11-6"></a><span class="co"># 该子库调取数据需要哪几个字段</span></span>
<span id="cb11-7"><a href="DataProcess.html#cb11-7"></a><span class="kw">getDimensions</span>(<span class="st">&#39;WITS&#39;</span>,<span class="st">&#39;WBG_WITS,DF_WITS_TradeStats_Tariff,1.0&#39;</span>)</span>
<span id="cb11-8"><a href="DataProcess.html#cb11-8"></a><span class="co"># 查看这个指标有几个选项 </span></span>
<span id="cb11-9"><a href="DataProcess.html#cb11-9"></a><span class="kw">getCodes</span>(<span class="st">&#39;WITS&#39;</span>,<span class="st">&#39;WBG_WITS,DF_WITS_TradeStats_Tariff,1.0&#39;</span>,<span class="st">&#39;INDICATOR&#39;</span>)</span>
<span id="cb11-10"><a href="DataProcess.html#cb11-10"></a><span class="co"># 查好了就可以下载</span></span>
<span id="cb11-11"><a href="DataProcess.html#cb11-11"></a>ans &lt;-<span class="st"> </span><span class="kw">getTimeSeries</span>(<span class="st">&#39;WITS&#39;</span>, <span class="st">&#39;DF_WITS_TradeStats_Tariff/A.CHN.WLD.01-05_Animal.MFN-WGHTD-AVRG&#39;</span>)</span>
<span id="cb11-12"><a href="DataProcess.html#cb11-12"></a><span class="co"># 你也可以调用图形窗口查阅命令</span></span>
<span id="cb11-13"><a href="DataProcess.html#cb11-13"></a><span class="kw">sdmxHelp</span>()</span></code></pre></div>
<p><code>IMF2</code>里面的<code>IFS</code>数据库里面有很多季度的宏观数据，如GDP，固定资本形成等</p>
</div>
<div id="前向后向线性和样条插值" class="section level2">
<h2><span class="header-section-number">3.3</span> 前向、后向、线性和样条插值</h2>
<ul>
<li><code>zoo</code>包
<ul>
<li><code>zoo::na.locf</code>缺省设置可以前向插，即缺失值等于前面的值。当将该函数的<code>fromLast</code>参数设为真时，即为后向插。</li>
<li><code>zoo:na.approx</code>可以线性插值但不能外推；<code>na.spline</code>可以样条插值；</li>
</ul></li>
<li><code>imputeTS</code>包,<code>imputeTS::na.locf</code>也可以，不过它只能对数值。它也有后向插值选项。</li>
</ul>
<div id="signal包" class="section level3">
<h3><span class="header-section-number">3.3.1</span> <code>signal</code>包</h3>
<p>它有一个插值函数<code>interp1</code>函数，比较好用：</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="DataProcess.html#cb12-1"></a><span class="kw">interp1</span>(x, y, xi, <span class="dt">method =</span> <span class="kw">c</span>(<span class="st">&quot;linear&quot;</span>, <span class="st">&quot;nearest&quot;</span>, <span class="st">&quot;pchip&quot;</span>, <span class="st">&quot;cubic&quot;</span>, <span class="st">&quot;spline&quot;</span>), </span>
<span id="cb12-2"><a href="DataProcess.html#cb12-2"></a>        <span class="dt">extrap =</span> <span class="ot">NA</span>, ...)</span></code></pre></div>
<p>它的参数说明如下</p>
<ul>
<li>x,y：vectors giving the coordinates of the points to be interpolated. x is assumed to be strictly monotonic.</li>
<li>xi：points at which to interpolate.</li>
</ul>
<p>method ：
one of “linear”, “nearest”, “pchip”, “cubic”, “spline”.</p>
<ul>
<li><p>‘nearest’: return nearest neighbour</p></li>
<li><p>‘linear’: linear interpolation from nearest neighbours</p></li>
<li><p>‘pchip’: piecewise cubic hermite interpolating polynomial</p></li>
<li><p>‘cubic’: cubic interpolation from four nearest neighbours</p></li>
<li><p>‘spline’: cubic spline interpolation–smooth first and second derivatives throughout the curve. for method=‘spline’, additional arguments passed to splinefun.
Details</p></li>
<li><p>extrap：
if TRUE or ‘extrap’, then extrapolate values beyond the endpoints. If extrap is a number, replace values beyond the endpoints with that number (defaults to NA).</p></li>
</ul>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="Julia.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="统计.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["CBook.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
